<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="部门状态" clearable>
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_normal_disable"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
   <!--操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--表格部门数据-->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="did"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="deptName" label="部门名称" width="260"></el-table-column>
      <el-table-column prop="orderNum" label="排序" width="200"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"

          >新增
          </el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级部门" prop="parentId">
              <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="部门状态">
              <el-radio-group v-model="form.status">
<!--                <el-radio-->
<!--                  v-for="dict in dict.type.sys_normal_disable"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.value"-->
<!--                >{{dict.label}}</el-radio>-->
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {listDept, getDept, deleteDept, updateDept, addDept} from '@/api/system/dept'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return{
      //部分列表数据
      deptList:[],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      queryParams:{
        deptName:undefined,
        status:undefined
      },
      //对话框标题 新增或是修改
      title:'',
      // 是否显示弹出层
      open: false,
      form:{
        did:undefined,
        parentId:undefined,
        deptName:undefined,
        orderNum:0,
        status:'0'
      },
      // 部门树选项
      deptOptions: [],
      // 表单校验
      rules:{
        parentId: [
          { required: true, message: "上级部门不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "部门顺序不能为空", trigger: "blur" }
        ]
        }
    }


  },
  methods:{
    /** 查询部门列表 */
    getList() {
      listDept(this.queryParams).then(data => {
        this.deptList = this.handleTree(data, "did");
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.did,
        label: node.deptName,
        children: node.children
      };
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
       getDept(row.did).then(data => {
        this.form = data;
        this.open = true;
        this.title = "修改部门";
         listDept().then(data => {
           this.deptOptions = this.handleTree(data, "did");
         });
      });
    },
    /** 新增按钮操作 */
    handleAdd(row) {

      //重置表单
      this.reset()
      //判断单击
      if(row != undefined)
      {
        this.form.parentId = row.did
      }
      this.open = true
      this.title = '新增部门'
      listDept().then(data => {
        this.deptOptions = this.handleTree(data, "did");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.deptName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return deleteDept(row.did);
      }).then(() => {
        this.getList();
        this.$message.success(`删除成功`)
      }).catch(function() {});

    },
    /** 搜索按钮操作 */
    handleQuery() {

      this.getList();
    },
    /**
     *  重置按钮操作
     */
    resetQuery()
    {

      this.queryParams={
       deptName:undefined,
       status:undefined
     }

      this.getList();

    },

    /**提交表单*/
    submitForm()
    {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.did != undefined) {
            updateDept(this.form).then(response => {
              this.$message.success(`修改成功`)
              this.open = false;
              this.getList();
            });
          } else {

            addDept(this.form).then(response => {
              this.$message.success(`新增部门成功`)
              this.open = false;
              this.getList();
            });
          }
        }
      })



    },
    // 表单重置
    reset() {
      this.form = {
        did: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    cancel()
    {
      this.open = false;
      this.reset();
    }
  },
  mounted() {
    this.getList();
  }
}
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>
